package com.yuan.reggie.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author yuanxingke
 * @date 2022/7/31 13:43
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R sqlHandlerException1(SQLIntegrityConstraintViolationException ex) {
        String message = ex.getMessage();

        if (message.contains("Duplicate entry")) {
            String username = message.split(" ")[2];
            return R.error(username + "已存在");
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(CustomerException.class)
    public R sqlHandlerException2(CustomerException ex) {
        String message = ex.getMessage();
        return R.error(message);
    }


}
